class CoefficientOfAssociation:
    # 初始化属性
    def __init__(self, age, fat):
        self.age = age
        self.fat = fat

    # 返回卡方值
    def chi_square_test(self):
        chi_square_value = 0
        for i in range(len(self.age)):
            chi_square_value += (self.age[i] - self.fat[i]) ** 2 / self.fat[i]
        return chi_square_value


    # 返回R
    def pearson_correlation_coefficient(self):
        # 计算平均值
        average_x = sum(self.age) / len(self.age)
        average_y = sum(self.fat) / len(self.fat)

        # 计算偏差乘积
        deviation_product = 0
        for i in range(len(self.age)):
            deviation_product += (self.age[i] - average_x) * (self.fat[i] - average_y)

        # 计算偏差平方和
        deviation_sum_of_squares_x = 0
        for i in range(len(self.age)):
            deviation_sum_of_squares_x += (self.age[i] - average_x) ** 2
        deviation_sum_of_squares_y = 0
        for i in range(len(self.age)):
            deviation_sum_of_squares_y += (self.fat[i] - average_y) ** 2

        # 计算标准差
        standard_deviation_x = pow(deviation_sum_of_squares_x, 0.5)
        standard_deviation_y = pow(deviation_sum_of_squares_y, 0.5)

        # 计算相关系数
        r = deviation_product / (standard_deviation_x * standard_deviation_y)

        # 打印相关性强度
        strength_of_association = ''
        if 0.1 <= abs(r) < 0.3:
            strength_of_association = 'very weak'
        elif 0.3 <= abs(r) < 0.5:
            strength_of_association = 'weak'
        elif 0.5 <= abs(r) < 1.0:
            strength_of_association = 'strong'
        print(f'相关性:{strength_of_association}')

        return round(r, 5)
